//
//  TicketsRecordCenterViewController.m
//  customer
//
//  Created by Holothurian on 2018/11/6.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "TicketsRecordCenterViewController.h"
#import "TicketCenterViewTableViewCell.h"
#import "TicketRecordDetailViewController.h"

static NSString *TicketTableCellID = @"TicketTableCellID";
@interface TicketsRecordCenterViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation TicketsRecordCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTicketsView];
    self.title = @"发票记录";
}

- (void)createTicketsView{
    AutoWeakSelf
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(NAVIGATION_BAR_HEIGHT);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_offset(F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0));
    }];
    self.refreshEnabled = YES;
    [self.tableView registerClass:[TicketCenterViewTableViewCell class] forCellReuseIdentifier:TicketTableCellID];
}

- (void)loadDataList{
    AutoWeakSelf
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
    [dict setObject:[NSString emptyStr:K_UserDefault_Read(CustomerUserID)] forKey:@"member_id"];
    [dict setObject:[NSString emptyStr:K_UserDefault_Read(CustomerUserToken)] forKey:@"member_token"];
    [dict setObject:[NSNumber numberWithInteger:self.page] forKey:@"page"];
    if (self.page == 1) { [self.dataList removeAllObjects]; }
    [self showLoading];
    [[QYNetManager sharedManager]POST: kGetMaintailOrderInvoices parameters:dict andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [weakSelf hideLoading];
        [weakSelf endReresh];
        id resultData = responseObject[@"data"];
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [weakSelf showErrorTipWithTitle:responseObject[@"error"]];
        }else if ([responseObject[@"status"] isEqualToString:@"ok"]){
            if (![resultData isKindOfClass:[NSNull class]] && [resultData isKindOfClass:[NSArray class]] && [resultData count]){
                if ([responseObject[@"total"] integerValue]<10 || ([responseObject[@"total"] integerValue] == self.dataList.count)) {
                    [weakSelf.tableView.mj_footer removeFromSuperview];
                }
                [self.dataList addObjectsFromArray:[NSArray yy_modelArrayWithClass:[TicketBaseModel class] json:resultData]];
                [weakSelf.tableView reloadData];
                 weakSelf.tableView.showEmpty = NO;
            }else if([resultData count] == 0){
                [weakSelf.dataList removeAllObjects];
                weakSelf.tableView.showEmpty = YES;
                [weakSelf showTipWithTitle:@"数据为空"];
            }
        }else{
            [weakSelf showErrorTipWithTitle:@"获取发票记录失败"];
        }
        
    } Failure:^(NSError *error) {
        [weakSelf endReresh];
        [weakSelf hideLoading];
        weakSelf.tableView.showEmpty = YES;
        [weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  [self.dataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TicketCenterViewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TicketTableCellID];
    [cell cellForLayoutTicketCell:self.dataList[indexPath.row]];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row >= [self.dataList count]) {
        return;
    }
    TicketRecordDetailViewController *detailVC = [[TicketRecordDetailViewController alloc]init];
    TicketBaseModel *ticketModel = self.dataList[indexPath.row];
    detailVC.ticketModel = ticketModel;
    TAB_PUSH(self, detailVC, YES);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 30)];
    backView.backgroundColor = [UIColor appColor];
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
